package com.haoma.controller;

import com.haoma.annotation.RequestCount;
import com.haoma.elasticsearch.ArticleEs;
import com.haoma.entity.Category;
import com.haoma.entity.Website;
import com.haoma.result.Result;
import com.haoma.service.ArticleService;
import com.haoma.service.impl.WebsiteServiceImpl;
import com.haoma.vo.ClassifyVo;
import com.haoma.vo.HotArticleVo;
import com.haoma.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Description:
 */
@RestController
@RequestMapping("/web/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private WebsiteServiceImpl websiteService;

    /**
     * 获取网站的个人信息
     *
     * @return
     */
    @GetMapping("/website")
    @RequestCount
    public Result getWebsite() {

        Website website = websiteService.getById(1);
        return Result.success(website);
    }

    /**
     * 获取文章归档信息
     *
     * @return
     */
    @GetMapping("/pigeonhole/{page}/{limit}")
    @RequestCount
    public Result getPigeonholeList(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<ArticleEs> pigeonholeList = articleService.getPigeonholeList(page, limit);
        return Result.success(pigeonholeList);
    }

    /**
     * 统计每月发布文章数量
     *
     * @return
     */
    @RequestCount
    @GetMapping("/pigeonhole/count")
    public Result countPigeonhole() {
        List<Integer> countList = articleService.countPigeonhole();
        return Result.success(countList);
    }

    /**
     * 根据id获取到文章详情
     *
     * @param id
     * @return
     */
    @RequestCount
    @GetMapping("/detail/{id}")
    public Result getArticleDetail(@PathVariable Long id) {
        ArticleEs articleEs = articleService.getArticleDetail(id);
        return Result.success(articleEs);
    }

    /**
     * 获取所有的标签和统计图
     *
     * @return
     */
    @RequestCount
    @GetMapping("/label")
    public Result getLabelList() {
        Map<String, List<Object>> stringListMap = articleService.getLabelList();
        return Result.success(stringListMap);
    }

    /**
     * 获取标签或者分类下的所有文章
     *
     * @param type
     * @param name
     * @return
     */
    @RequestCount
    @GetMapping("/entire/{page}/{limit}")
    public Result getEntireList(@PathVariable Integer page, @PathVariable Integer limit, Integer type, String name) {
        List<HotArticleVo> hotArticleVoList = articleService.getEntireList(page, limit, type, name);
        return Result.success(hotArticleVoList);
    }


    // ========分类相关接口

    /**
     * 获取文章分类和排序数据
     *
     * @return
     */

    @RequestCount
    @GetMapping("/classify")
    public Result getClassifyList() {
        List<ClassifyVo> classifyVoList = articleService.getClassifyList();
        return Result.success(classifyVoList);
    }


    // ========首页相关接口

    /**
     * 文章分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestCount
    @GetMapping("/{page}/{limit}")
    public Result pageArticle(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<ArticleEs> iPage = articleService.articlePage(page, limit);
        return Result.success(iPage);
    }

    /**
     * 获取所有的分类
     *
     * @return
     */
    @GetMapping("/group")
    public Result getCategory() {
        List<Category> categoryList = articleService.getCategoryList();
        return Result.success(categoryList);
    }

    /**
     * 获取最火的文章
     *
     * @return
     */
    @GetMapping("/hot")
    public Result getHotArticle() {
        List<HotArticleVo> hotArticleVos = articleService.getHotArticle();
        return Result.success(hotArticleVos);
    }

    /**
     * 获取推荐文章
     *
     * @return
     */
    @GetMapping("/recommend")
    public Result getRecommendArticle() {
        List<HotArticleVo> hotArticleVos = articleService.getRecommendArticle();
        return Result.success(hotArticleVos);
    }

    /**
     * 统计数量
     *
     * @return
     */
    @GetMapping("/count")
    public Result getCount() {
        Map<String, Object> countMap = articleService.getCount();
        return Result.success(countMap);
    }

    /**
     * 搜索
     *
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam String value) throws IOException {
        List<SearchVo> searchVoList = articleService.search(value);
        return Result.success(searchVoList);
    }
}
